package com.gopay.scheduler.job;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.cps.dao.wallet.tyy.WaTyyInviteInfoDao;
import com.gopay.common.domain.cust.CustPersonInfo;
import com.gopay.common.domain.dps.WaTyyInviteInfo;
import com.gopay.common.user.dao.CustPersonInfoQueryDAO;
import com.gopay.common.util.DateUtils;
import com.gopay.scheduler.GopayBaseJob;

/**
 * 根据WA_TYY_INVITE_INFO(天溢盈邀请记录表)中未注册的手机号，
 * 查找cust_person_info,若查找到相关记录，更新WA_TYY_INVITE_INFO(天溢盈邀请记录表)cust_id、注册状态、注册时间、邀请成功标识。
 * 
 * @author tangyang
 *
 */
public class WaTyyInvitedRegisterJob extends GopayBaseJob {
    private Logger logger = LoggerFactory.getLogger(WaTyyInvitedRegisterJob.class);

    private final int pageSize = 1000;
    
    @Resource(name = "waTyyInviteInfoDao")
    private WaTyyInviteInfoDao waTyyInviteInfoDao;

    
    @Resource(name = "custPersonInfoQueryDao")
    private CustPersonInfoQueryDAO custPersonInfoQueryDao;
    @Override
    protected void doJobInternal() {
        // TODO Auto-generated method stub
        // 处理未注册的邀请记录，WA_TYY_INVITE_INFO(天溢盈邀请记录表)中未注册的手机号
        //查询未注册且邀请成功的数据
        int registerNum = waTyyInviteInfoDao.findCountByIsRegister("0");
        logger.info("天溢盈邀请未注册用户" + registerNum);
        if(registerNum>pageSize){
            int N = registerNum/pageSize;
            int M = registerNum%pageSize;
            if(M>0){
                N = N + 1; 
            }
            for(int i=0; i<N; i++){
                List<WaTyyInviteInfo> InviteInfos = waTyyInviteInfoDao.findByIsRegister("0",i+1,pageSize);
                updateIsRegisters(InviteInfos);
            }
        }else{
            List<WaTyyInviteInfo> InviteInfos = waTyyInviteInfoDao.findByIsRegister("0",1,pageSize);
            updateIsRegisters(InviteInfos);
        }
        //查询已注册且邀请成功的数据
        int inviteStatusSuc = waTyyInviteInfoDao.findCountByIviteStatus("1");
        logger.info("天溢盈邀请成功用户" + inviteStatusSuc);
        //处理邀请成功的记录，是否存在变为邀请失败的处理
        if(inviteStatusSuc>pageSize){
            int N = inviteStatusSuc/pageSize;
            int M = inviteStatusSuc%pageSize;
            if(M>0){
                N = N + 1; 
            }
            for(int i=0; i<N; i++){
                List<WaTyyInviteInfo> InviteInfos = waTyyInviteInfoDao.findByIviteStatus("1",i+1,pageSize);
                updateInviteStatus(InviteInfos,"1");
            }
        }else{
            List<WaTyyInviteInfo> InviteInfos = waTyyInviteInfoDao.findByIviteStatus("1",1,pageSize);
            updateInviteStatus(InviteInfos,"1");
        }
        //查询已注册且邀请失败的数据
        int inviteStatusFal = waTyyInviteInfoDao.findCountByIviteStatus("0");
        logger.info("天溢盈邀请失败用户" + inviteStatusFal);
        //处理邀请失败的记录，是否存在变为邀请成功的处理
        if(inviteStatusFal>pageSize){
            int N = inviteStatusFal/pageSize;
            int M = inviteStatusFal%pageSize;
            if(M>0){
                N = N + 1; 
            }
            for(int i=0; i<N; i++){
                List<WaTyyInviteInfo> InviteInfos = waTyyInviteInfoDao.findByIviteStatus("0",i+1,pageSize);
                updateInviteStatus(InviteInfos,"0");
            }
        }else{
            List<WaTyyInviteInfo> InviteInfos = waTyyInviteInfoDao.findByIviteStatus("0",1,pageSize);
            updateInviteStatus(InviteInfos,"0");
        }
    }

    /**
     * 对未注册且邀请成功的数据进行处理
     * @param InviteInfos
     */
    private void updateIsRegisters(List<WaTyyInviteInfo> InviteInfos){
        if (InviteInfos != null && InviteInfos.size() > 0) {
            for (WaTyyInviteInfo inviteInfo : InviteInfos) {
                boolean updateFlag = false;
                CustPersonInfo inviteCustPersonInfo = custPersonInfoQueryDao.find(CustPersonInfo.class,inviteInfo.getInviteCustId());
                //如果邀请人已离职，更新邀请状态为邀请失败
                if(StringUtils.equals("0",inviteCustPersonInfo.getIsEmployees())){
                    inviteInfo.setRemark("邀请人已离职");
                    inviteInfo.setInviteStatus("0");
                    updateFlag = true;
                }
                //如果邀请人的手机号变更,更新邀请人手机号
                if(!StringUtils.equals(inviteInfo.getInviteCustMob(), inviteCustPersonInfo.getCustMob())){
                    inviteInfo.setInviteCustMob(inviteCustPersonInfo.getCustMob());
                    updateFlag = true;
                }
                // 根据手机号查询已实名已认证cust_person_info
                CustPersonInfo receiveCustPersonInfo = custPersonInfoQueryDao.findByCustMobmobile(inviteInfo.getReceiveCustMob());
                //受邀人已注册
                if(receiveCustPersonInfo != null 
                        && StringUtils.equals("1",inviteCustPersonInfo.getIsEmployees())){
                 // 更新WA_TYY_INVITE_INFO表
                    inviteInfo.setIsRegister("1");//更新是否注册标志
                    inviteInfo.setReceiveCustId(receiveCustPersonInfo.getCustId());//受邀人ID
                    inviteInfo.setRegisterTs(receiveCustPersonInfo.getCreatedTs());
                    //受邀人为国付宝员工，邀请失败
                    if(StringUtils.equals("1", receiveCustPersonInfo.getIsEmployees())){
                        logger.info("天溢盈邀请用户修改为邀请失败" + inviteInfo);
                        inviteInfo.setInviteStatus("0");
                        inviteInfo.setRemark("国付宝员工");
                        updateFlag = true;
                    }else{//受邀人非国付宝员工，邀请成功
                        logger.info("天溢盈邀请用户修改为邀请成功" + inviteInfo);
                        inviteInfo.setInviteStatus("1");
                        updateFlag = true;
                    }
                }
                if(updateFlag){
                    inviteInfo.setLastUpdTs(DateUtils.getSystemDate());
                    waTyyInviteInfoDao.update(inviteInfo);
                }
            }
        }
    }
    /**
      * 对注册且邀请失败（或邀请成功）的数据做处理
      */
    private void updateInviteStatus(List<WaTyyInviteInfo> InviteInfos, String inviteStatus ){
        if (InviteInfos != null && InviteInfos.size() > 0) {
            for (WaTyyInviteInfo inviteInfo : InviteInfos) {
                boolean updateFlag = false;
                // 根据手机号查询已实名已认证cust_person_info
                CustPersonInfo inviteCustPersonInfo = custPersonInfoQueryDao.find(CustPersonInfo.class, inviteInfo.getInviteCustId());
                //如果邀请人已离职，更新邀请状态为邀请失败
                if( StringUtils.equals(inviteStatus, "1") 
                        && StringUtils.equals(inviteCustPersonInfo.getIsEmployees(), "0")){
                    inviteInfo.setInviteStatus("0");
                    inviteInfo.setRemark("邀请人已离职 ");
                    updateFlag = true;
                }
                //如果邀请人的手机号变更,更新邀请人手机号
                if(!StringUtils.equals(inviteInfo.getInviteCustMob(), inviteCustPersonInfo.getCustMob())){
                    inviteInfo.setInviteCustMob(inviteCustPersonInfo.getCustMob());
                    updateFlag = true;
                }
                
                CustPersonInfo receiveCustPersonInfo = custPersonInfoQueryDao.find(CustPersonInfo.class, inviteInfo.getReceiveCustId());
                //如果为邀请成功的记录，被邀请人变为国付宝员工，且邀请人仍为国服宝员工，则更新为邀请失败
                if( StringUtils.equals(inviteStatus, "1")
                        && StringUtils.equals(receiveCustPersonInfo.getIsEmployees(), "1")
                        && StringUtils.equals(inviteCustPersonInfo.getIsEmployees(), "1")){
                    inviteInfo.setInviteStatus("0");
                    inviteInfo.setRemark("国付宝员工");
                    updateFlag = true;
                }//如果为邀请失败的记录，被邀请人变为非国付宝员工，且邀请人仍为国服宝员工，则更新为邀请成功
                else if(StringUtils.equals(inviteStatus, "0")
                        && StringUtils.equals(receiveCustPersonInfo.getIsEmployees(), "0")
                        && StringUtils.equals(inviteCustPersonInfo.getIsEmployees(), "1")){
                    inviteInfo.setInviteStatus("1");
                    inviteInfo.setRemark("");
                    updateFlag = true;
                }
                //如果受邀人的手机号注册后变更
                if(!StringUtils.equals(inviteInfo.getReceiveCustMob(), receiveCustPersonInfo.getCustMob())){
                    inviteInfo.setReceiveCustMob(receiveCustPersonInfo.getCustMob());
                    updateFlag = true;
                }
                if(updateFlag){
                    inviteInfo.setLastUpdTs(DateUtils.getSystemDate());
                    waTyyInviteInfoDao.update(inviteInfo);
                }
            }
        }
    }
}
